package com.zyk.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangsan
 * @date 2021/5/29 12:28
 */
public class C560 {


    public static int subarraySum(int[] nums, int k) {
        int N = nums.length;
        int cnt = 0, sum = 0;
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        for (int i = 0; i < N; i++) {
            sum += nums[i];
            if (map.containsKey(sum - k)) cnt += map.get(sum - k);
            map.put(sum, map.getOrDefault(sum, 0) + 1);
        }
        return cnt;
    }

    public static void main(String[] args) {
        int[] nums = {1};
        int k = 1;
        System.out.println(subarraySum(nums, k));
    }

}
